package api;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author DCSGO
 * @version 1.0
 * 获取计算机的主机名和IP地址相关API
 */
public class Api_ {
    public static void main(String[] args) throws UnknownHostException {
        //1.获取本机的 InetAddress 对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);//DCSGO/192.168.163.1

        //2.根据主机名获取 InetAddress 对象
        InetAddress host1 = InetAddress.getByName("DCSGO");
        System.out.println(host1);//DCSGO/192.168.163.1

        //3.根据域名获取 InetAddress 对象
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println(host2);//www.baidu.com/182.61.200.7

        //4.通过 InetAddress 对象获取其对应的主机地址
        System.out.println(host2.getHostAddress());//182.61.200.7

        //5.通过 InetAddress 对象获取其对应的主机名/域名
        System.out.println(host2.getHostName());//www.baidu.com
    }
}
